package tests;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Ignore;
import org.junit.Test;

import recipe.Ingredient;
import recipe.RecipeData;

public class TestRecipeData {

	@Test
	public void testRecipeData() {
		RecipeData rec = new RecipeData();
		assertTrue(
				rec.getRecipeName().isEmpty() &&
				rec.getCategory().isEmpty() &&
				rec.getImageFileName().isEmpty() &&
				rec.getIngredients().isEmpty() &&
				rec.getInstructions().isEmpty() &&
				rec.getTags().isEmpty()
				);
	}

	@Test
	public void testGetRecipeName() {
		String nm = "Name";
		RecipeData rec = new RecipeData();
		rec.setRecipeName(nm);
		assertTrue(nm.equals(rec.getRecipeName()));
	}

	@Test
	public void testSetRecipeName() {
		String nm = "Name";
		RecipeData rec = new RecipeData();
		rec.setRecipeName(nm);
		assertTrue(rec.getRecipeName().equals(nm));
	}

	@Test
	public void testGetImageFileName() {
		String img = "Image";
		RecipeData rec = new RecipeData();
		rec.setImageFileName(img);
		assertTrue(img.equals(rec.getImageFileName()));
	}

	@Test
	public void testSetImageFileName() {
		String img = "Image";
		RecipeData rec = new RecipeData();
		rec.setImageFileName(img);
		assertTrue(rec.getImageFileName().equals(img));
	}

	@Test
	public void testGetCategory() {
		String cat = "Category";
		RecipeData rec = new RecipeData();
		rec.setCategory(cat);
		assertTrue(cat.equals(rec.getCategory()));
	}

	@Test
	public void testSetCategory() {
		String cat = "Category";
		RecipeData rec = new RecipeData();
		rec.setCategory(cat);
		assertTrue(rec.getCategory().equals(cat));
	}

	@Test
	public void testGetIngredients() {
		String ing = "Ingredients";
		RecipeData rec = new RecipeData();
		rec.setIngredients(ing);
		assertTrue(ing.equals(rec.getIngredients()));
	}

	@Test
	public void testSetIngredients() {
		String ing = "Ingredients";
		RecipeData rec = new RecipeData();
		rec.setIngredients(ing);
		assertTrue(rec.getIngredients().equals(ing));
	}
	
	@Test
	@Ignore
	public void testGetListOfIngredients(){
		
	}
	
	@Test
	public void testGetListOfIngredientsAsString(){
		StringBuilder ing = new StringBuilder();
		RecipeData rec = new RecipeData();
		
		rec.addIngredient("water", "1", "c");
		rec.addIngredient("beef", "2", "oz");
		rec.addIngredient("stock", "500", "mL");
		
		ing.append("1 c water\n");
		ing.append("2 oz beef\n");
		ing.append("500 mL stock");
		
		assertTrue(ing.toString().equals(rec.getListOfIngredientsAsString()));
	}
	
	@Test
	public void testSetListOfIngredients(){
		StringBuilder ing = new StringBuilder();
		RecipeData rec = new RecipeData();
		ArrayList<Ingredient> list = new ArrayList<Ingredient>();
		list.add(new Ingredient("water", "1", "c"));
		list.add(new Ingredient("beef", "2", "oz"));
		list.add(new Ingredient("stock", "500", "mL"));
		
		ing.append("1 c water\n");
		ing.append("2 oz beef\n");
		ing.append("500 mL stock");
		
		rec.setListOfIngredients(ing.toString());
		
		assertTrue(compareListOfIngredients(rec.getListOfIngredients(), list));
	}
	
	private boolean compareListOfIngredients(
			ArrayList<Ingredient> first, ArrayList<Ingredient> second){
		boolean isSame = true;
		Ingredient i1 = null;
		Ingredient i2 = null;
		
		if(first.size() != second.size()){
			isSame = false;
		}
		else{
			for(int i = 0; i < first.size(); i++){
				i1 = first.get(i);
				i2 = second.get(i);
				if(i1.toString().compareTo(i2.toString()) != 0){
					isSame = false;
				}
			}
		}
		
		return isSame;
	}

	@Test
	public void testGetInstructions() {
		String instr = "Instructions";
		RecipeData rec = new RecipeData();
		rec.setInstructions(instr);
		assertTrue(instr.equals(rec.getInstructions()));
	}

	@Test
	public void testSetInstructions() {
		String instr = "Instructions";
		RecipeData rec = new RecipeData();
		rec.setInstructions(instr);
		assertTrue(rec.getInstructions().equals(instr));
	}

	@Test
	public void testGetTags() {
		String tg = "Tags";
		RecipeData rec = new RecipeData();
		rec.setTags(tg);
		assertTrue(tg.equals(rec.getTags()));
	}

	@Test
	public void testSetTags() {
		String tg = "Tags";
		RecipeData rec = new RecipeData();
		rec.setTags(tg);
		assertTrue(rec.getTags().equals(tg));
	}

	@Test
	@Ignore
	public void testConvertUnits() {
		fail("Not yet implemented");
	}

	@Test
	public void testScaleAmount() {
		RecipeData r = new RecipeData();
		ArrayList<Ingredient> l = null;
		r.addIngredient("Eggs", "2", "");
		r.addIngredient("Milk", "1", "Tbsp");
		
		r.scaleAmount(2);
		
		l = r.getListOfIngredients();
		
		assertEquals("Should be equal", "4.00", l.get(0).getAmount());
		assertEquals("Should be equal", "2.00", l.get(1).getAmount());
	}

}
